package com.bwt.usermanage.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import com.bwt.usermanage.InsertOrEditActivity;
import com.bwt.usermanage.R;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * @author Tao
 * @date 2024/3/24 22:27
 */
public class ViewUtil {

    public static final String GAODE_PACKAGENAME = "com.autonavi.minimap";

    public static void hideOneInputMethod(Activity activity, View view) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }



    /***

     * 获取应用程序名称。

     * @param context

     * @return

     */

    public static String getAppName(Context context) {

        try {

            PackageManager packageManager = context.getPackageManager();

            // PackageInfo packageInfo = packageManager.getPackageInfo(
            //
            //         context.getPackageName(), 0);
            //
            // int labelRes = packageInfo.applicationInfo.labelRes;


            /*context.getString(R.string.app_name)*/

            ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

            String appName = appInfo.loadLabel(context.getPackageManager()) + "";

            Log.d("稳定、可靠获取App名称", appName);


            return appName;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;


    }


    public static boolean isDownAndjumjp(Context context, String packgeName) {
        if (isInstalledPackage(context, packgeName)) {
            return true;
            // 已安装高德地图应用
        } else {
            ToastUtil.show(context, "请先安装高德地图");
            // goToGaoDeDownloadPage(context,packgeName);
            return false;
        }

    }

    /**
     * 根据包名判断是否安装应用
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isInstalledPackage(Context context,String packageName){
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals(packageName)) {
                    return true;
                }
            }
        }
        return false;
    }
/*    public static boolean isAppInstalled(Context context, String packageName) {
        PackageManager pm = context.getPackageManager();
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }*/

    public static void goToGaoDeDownloadPage(Context context, String packgeName) {
        Uri uri = Uri.parse("market://details?id=" + packgeName);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }


        public static RadioButton getRGSelected(RadioGroup radioGroup ) {

        RadioButton radioButton = null;
        for(int i = 0 ;i < radioGroup.getChildCount();i++){
            RadioButton rb = (RadioButton)radioGroup.getChildAt(i);
            if(rb.isChecked()){
                radioButton = rb;
                break;
            }
        }

        return radioButton;
    }

    public static void setSpinnerSlection(String item, String[] itemList, Spinner spinner) {
        for (int i = 0; i < itemList.length; i++) {
            if (StringUtils.isNotBlank(item) && item.equals(itemList[i])) {
                spinner.setSelection(i);
            }
        }
    }
    public static void setRadioGroup(String param, RadioGroup radioGroup, int... ids) {
        if (StringUtils.isBlank(param)) {
            return;
        }
        for (int i = 0; i < ids.length; i++) {
            if (i+1 == Integer.parseInt(param)) {
                radioGroup.check(ids[i]);
            }
        }
    }
    public static void showImageDialog(Context context, ImageView imageView) {

        final Dialog dialog = new Dialog(context);
        ImageView image = new ImageView(context);
        Drawable drawable = imageView.getDrawable();
        if (drawable == null) {
            return;
        }
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        if (bitmap == null) {
            return;
        }
        image.setImageBitmap(bitmap);
        dialog.setContentView(image);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.show();
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });


    }


    public static byte[] getByteArr(ImageView iv) {

        Drawable drawable = iv.getDrawable();
        if (drawable == null) {
            return null;
        }
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        return byteArray;
    }

    public static Bitmap getBitmap(byte[] byteArray) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
        return bitmap;
    }

    public static Bitmap getBitmap(ImageView iv) {
        Drawable drawable = iv.getDrawable();
        if (drawable == null) {
            return null;
        }
        return ((BitmapDrawable) drawable).getBitmap();
    }
}
